#pragma once
#include<vector>
#include<iostream>

#define SIZE 15

struct pos {
	int row;
	int col;
};

class robot{
public:
    robot():_score_recond(SIZE, std::vector<int>(SIZE, 0))
	{}
    ~robot(){}

    //评分函数
	void re_calculate_score(const std::vector<std::vector<int>>& board) {
		
		//重新初始化评分记录
		for (int i = 0; i < SIZE; i++)
			for (int j = 0; j < SIZE; j++)
			{
				_score_recond[i][j] = 0;
			}
		

		for (int row = 0; row < SIZE; row++)
		{
			for (int col = 0; col < SIZE; col++)
			{
				//已经走过的棋子不需要评分
				if (board[row][col] != 0) continue;
				_empty_num = 0;

				//假设当前走的是黑子或白子,分别对4个方向进行枚举统计
				for (int y = -1; y <= 0; y++)
				{
					for (int x = -1; x <= 1; x++)
					{
						if (y == 0 && x != -1) continue;
						if (x == 0 && y == 0) continue;
						_empty_num = 0;
						int count_black = line_num(row, col, y, x, CHESS_WHITE, board);
						int score_black = judge_score(row, col, count_black, CHESS_WHITE);
						_score_recond[row][col] += score_black;

						_empty_num = 0;
						int count_white = line_num(row, col, y, x, CHESS_BLACK, board);
						int score_white = judge_score(row, col, count_white, CHESS_BLACK);
						_score_recond[row][col] += score_white;
					}
				}
				

			}
		}
	}

    pos get_max_score_pos()
	{
		int max_score = 0;
		pos tmp;
		for (int row = 0; row < SIZE; row++)
		{
			for (int col = 0; col < SIZE; col++)
			{
				if (_score_recond[row][col] > max_score)
				{
					max_score = _score_recond[row][col];
				}
			}
		}

        std::vector<pos> rand_pos;
        int rand_time = rand()%100000;
        //增加棋子的随机性
        for (int row = 0; row < SIZE; row++)
		{
			for (int col = 0; col < SIZE; col++)
			{
				if (_score_recond[row][col] == max_score)
				{
					tmp.row = row;
                    tmp.col = col;
                    rand_pos.push_back(tmp);
				}
			}
		}

		return rand_pos[ rand_time % rand_pos.size()];
	}
private:
    int line_num(int row, int col, int row_offset, int col_offset, int color, const std::vector<std::vector<int>>& board)
	{
		int count = 1;
		int cur_row = row + row_offset;
		int cur_col = col + col_offset;
		while (cur_row >= 0 && cur_row < SIZE && cur_col >= 0
			&& cur_col < SIZE && board[cur_row][cur_col] == color)
		{
			count++;
			cur_row += row_offset;
			cur_col += col_offset;
		}

		if (board[row][col] == 0) _empty_num++;

		cur_row = row - row_offset;
		cur_col = col - col_offset;
		while (cur_row >= 0 && cur_row < SIZE && cur_col >= 0
			&& cur_col < SIZE && board[cur_row][cur_col] == color)
		{
			count++;
			cur_row -= row_offset;
			cur_col -= col_offset;
		}

		if (board[row][col] == 0) _empty_num++;
		return count;
	}

    int judge_score(int row, int col, int count, int color)
	{
		if (count == 1 && color == CHESS_BLACK)
			return 5;
		else if (count == 2)
		{
			return 10;
		}
		else if (count == 3)
		{
			if (_empty_num == 1)
				return color == CHESS_WHITE ? 30 : 25;
			else if (_empty_num == 2)
				return color == CHESS_WHITE ? 40 : 50;
		}
		else if (count == 4)
		{
			if (_empty_num == 1)
				return color == CHESS_WHITE ? 60 : 55;
			else if (_empty_num == 2)
				return color == CHESS_WHITE ? 200 : 10000;
		}
		else if (count == 5)
		{
			return color == CHESS_WHITE ? 20000 : 30000;
		}

		return 0;
	}

private:
    int uid = -1;
	int _empty_num = 0;
	std::vector<std::vector<int>> _score_recond;
};